package no.nordicsemi.android.blinky.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据操作工具类
 */
public class DataHandlerUtils {

    /**
     * 拼接字节数组
     * @param data1
     * @param data2
     * @return
     */
    public static byte[] addBytes(byte[] data1, byte[] data2) {
        byte[] data3 = data1;
        if (data2 != null && data2.length != 0){
            data3 = new byte[data1.length + data2.length];
            System.arraycopy(data1, 0, data3, 0, data1.length);
            System.arraycopy(data2, 0, data3, data1.length, data2.length);
        }
        return data3;
    }


    /**
     * byte[] --> 十六进制的字符串
     * @param bytes
     * @return
     */
    public static String bytesToHexStr(byte[] bytes){
        StringBuilder stringBuilder = new StringBuilder("");
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(" " + 0 + hv);
            }else{
                stringBuilder.append(" " + hv);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 字节数组转化成集合
     */
    public static List<Integer> bytesToArrayList(byte[] bytes){
        List<Integer> datas = new ArrayList<>();
        for (int i = 0; i < bytes.length; i++) {
            datas.add(bytes[i] & 0xff);
        }
        return datas;
    }

    public static byte[] handleMessageBytes(byte[] bytes) {
        // ab 00 57 ff 72 80 07 02 e6 b5 8b e8 af 95 e6 b6 88 e6 81 af

//        00| e9 80 9a e7 9f a5 77 6f 63 61 6e 69 6a 66 6b 61 6c 64 6a
//        01| 66 61 6b 6c 64 66 6a 6b 6c 61 6a 66 6b 61 6c 64 6a 64 66
//        02| 6c 6b 61 6a 66 6c 6b 61 6a 66 6c 3b 6b 61 6a 66 6b 6c 61
//        03| 64 73 6a 66 6c 6b 64 73 6a 66 6b 6c 61
        List<Integer> datas = DataHandlerUtils.bytesToArrayList(bytes);
        if (datas.get(0) == 0xAB && datas.get(4) == 0x72) {
            //消息通知的数据需要处理成以上格式
            if (bytes.length > 20) {
                byte[] firstPacket = new byte[20];
                System.arraycopy(bytes, 0, firstPacket, 0, 20);

                byte[] otherPackets = new byte[bytes.length - 20];
                System.arraycopy(bytes, 20, otherPackets, 0, bytes.length - 20);
                //后面的所有数据
                ArrayList<byte[]> bytes1 = new ArrayList<>();
                for (int i = 0; i < otherPackets.length / 19 + 1; i++) {
                    byte[] temp = new byte[19];
                    byte[] temp1 = new byte[otherPackets.length % 19];
                    if (i < otherPackets.length / 19) {
                        System.arraycopy(otherPackets, i * 19, temp, 0, 19);
                        bytes1.add(temp);
                    } else {
                        System.arraycopy(otherPackets, i * 19, temp1, 0, otherPackets.length % 19);
                        bytes1.add(temp1);
                    }
                }

                //加上序号
                ArrayList<byte[]> newBytes = new ArrayList<>();
                for (int i = 0; i < bytes1.size(); i++) {
                    byte[] bytes2 = DataHandlerUtils.addBytes(new byte[]{(byte) i}, bytes1.get(i));
                    newBytes.add(bytes2);
                }

                //全部拼接起来
                bytes = new byte[20 + 20 * (otherPackets.length / 19) + otherPackets.length % 19+1];
                System.arraycopy(firstPacket, 0, bytes, 0, 20);
                for (int i = 0; i < newBytes.size(); i++) {
                    if (i < newBytes.size() - 1) {
                        System.arraycopy(newBytes.get(i), 0, bytes, 20 * (i + 1), 20);
                    } else {
                        System.arraycopy(newBytes.get(i), 0, bytes, 20 * (i + 1), otherPackets.length % 19 + 1);
                    }
                }

            }
        }


        return bytes;
    }
}